home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5034 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.9 KB  |  106 lines

  1. Path: kbad.eglin.af.mil!rpi!not-for-mail
  2. From: David Carr <davidc@marine.simrad.no>
  3. Newsgroups: comp.lang.c++,comp.lang.c++.moderated,comp.lang.c.moderated
  4. Subject: Q: Rigorous coding using #if !defined(...) and #include
  5. Date: 31 Jan 1996 15:22:36 -0000
  6. Organization: EUnet Norway
  7. Sender: cppmods@netlab.cs.rpi.edu
  8. Approved: kanze@lts.sel.alcatel.de
  9. Message-ID: <4eo1fs$rr3@netlab.cs.rpi.edu>
  10. NNTP-Posting-Host: netlab.cs.rpi.edu
  11. X-Original-Date: Wed, 31 Jan 1996 15:57:28 +0100
  12.  
  13. Q: Rigorous coding w #if !defined
  14.  
  15. Hi!
  16.  
  17. I have been used to following rigorous coding standards.  This 
  18. has translated
  19. into a clear separation between H and source files, and making 
  20. thorough
  21. use of compiler directives.  In header files, I have used the 
  22. notation;
  23.  
  24. #if !defined (__MYCLASS_H)
  25. #define (__MYCLASS_H)
  26.  
  27. #include "Other.h"
  28.  
  29. class MyClass
  30. {
  31.     ...// Uses something in Other.h
  32. };
  33.  
  34. #endif
  35.  
  36. where Other.h is the header file(s) that define the classes, 
  37. types, and/or
  38. operations used within the MyClass.h file.  This approach also 
  39. explicitly
  40. binds all the dependencies between code (i.e. so builds are 
  41. correct and up
  42. to date).
  43.  
  44. Okay, nuff of the preamble.  The problem is this.  I have 
  45. another class;
  46.  
  47. #if !defined (__OTHER_H)
  48. #define (__OTHER_H)
  49.  
  50. #include "MyClass.h"
  51.  
  52. class Other
  53. {
  54.     ...// Uses something in MyClass.h
  55. };
  56.  
  57. #endif
  58.  
  59. and in my source for MyClass.cpp, MyClass.h is obviously 
  60. included.  Now,
  61. when I try to compile MyClass.cpp, MyClass.h is included and 
  62. __MYCLASS_H 
  63. becomes defined.  Then, MyClass.h #includes Other.h, thus 
  64. defining __OTHER_H.
  65. Now, Other.h #includes MyClass.h.  As __MYCLASS_H has already 
  66. been defined,
  67. the code in MyClass.h is not included.  Returning to Other.h, 
  68. this code
  69. references something in MyClass.h (i.e. its class) and a 
  70. compiler error is
  71. generated because the symbol/identifier/whatever is unknown.
  72.  
  73. So, upon looking at VC++ examples and the code automatically 
  74. generated, it
  75. seems that VC++ solves this circular problem by not using 
  76. #includes within
  77. H files (at least not normally).  The dependency relationships 
  78. are all 
  79. determined within the CPP file.  I DO NOT like this because then 
  80. if I want to
  81. use MyClass elsewhere, I am responsible for tracing through and 
  82. #including
  83. all the required dependencies.  To use the objects in an H file, 
  84. I just want
  85. to include the file without any concern for ordering of the H 
  86. files or being
  87. responsible for creating its dependencies.
  88.  
  89. Now that most people who aren't interested have fallen asleep:
  90.  
  91. What is the preferred method for writing rigorous code and 
  92. solving this
  93. problem? 
  94. Any suggestions as to how to make H files a complete interface?
  95.  
  96. Thanks in advance,
  97.  
  98. David Carr
  99. - -Canuck displaced in Bergen, Norway-
  100. davidc@simrad.marine.no
  101.  
  102.       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
  103.       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
  104.       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
  105.       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
  106.